package by.ad;

import java.io.File;
import java.util.Arrays;

public abstract class Utils 
{
  //
  // http://blog.redfin.com/devblog/2007/08/getting_the_time_zone_from_a_web_browser.html
  //
  public static String getTimezoneName(final long offsetSummer, final long offsetWinter)
  {
    if (-660 == offsetSummer && -660 == offsetWinter) return "Pacific/Midway";
    if (-600 == offsetSummer && -600 == offsetWinter) return "Pacific/Tahiti";
    if (-570 == offsetSummer && -570 == offsetWinter) return "Pacific/Marquesas";
    if (-540 == offsetSummer && -600 == offsetWinter) return "America/Adak";
    if (-540 == offsetSummer && -540 == offsetWinter) return "Pacific/Gambier";
    if (-480 == offsetSummer && -540 == offsetWinter) return "US/Alaska";
    if (-480 == offsetSummer && -480 == offsetWinter) return "Pacific/Pitcairn";
    if (-420 == offsetSummer && -480 == offsetWinter) return "US/Pacific";
    if (-420 == offsetSummer && -420 == offsetWinter) return "US/Arizona";
    if (-360 == offsetSummer && -420 == offsetWinter) return "US/Mountain";
    if (-360 == offsetSummer && -360 == offsetWinter) return "America/Guatemala";
    if (-360 == offsetSummer && -300 == offsetWinter) return "Pacific/Easter";
    if (-300 == offsetSummer && -360 == offsetWinter) return "US/Central";
    if (-300 == offsetSummer && -300 == offsetWinter) return "America/Bogota";
    if (-240 == offsetSummer && -300 == offsetWinter) return "US/Eastern";
    if (-240 == offsetSummer && -240 == offsetWinter) return "America/Caracas";
    if (-240 == offsetSummer && -180 == offsetWinter) return "America/Santiago";
    if (-180 == offsetSummer && -240 == offsetWinter) return "Canada/Atlantic";
    if (-180 == offsetSummer && -180 == offsetWinter) return "America/Montevideo";
    if (-180 == offsetSummer && -120 == offsetWinter) return "America/Sao_Paulo";
    if (-150 == offsetSummer && -210 == offsetWinter) return "America/St_Johns";
    if (-120 == offsetSummer && -180 == offsetWinter) return "America/Godthab";
    if (-120 == offsetSummer && -120 == offsetWinter) return "America/Noronha";
    if (-60 == offsetSummer && -60 == offsetWinter) return "Atlantic/Cape_Verde";
    if (0 == offsetSummer && -60 == offsetWinter) return "Atlantic/Azores";
    if (0 == offsetSummer && 0 == offsetWinter) return "Africa/Casablanca";
    if (60 == offsetSummer && 0 == offsetWinter) return "Europe/London";
    if (60 == offsetSummer && 60 == offsetWinter) return "Africa/Algiers";
    if (60 == offsetSummer && 120 == offsetWinter) return "Africa/Windhoek";
    if (120 == offsetSummer && 60 == offsetWinter) return "Europe/Amsterdam";
    if (120 == offsetSummer && 120 == offsetWinter) return "Africa/Harare";
    if (180 == offsetSummer && 120 == offsetWinter) return "Europe/Athens";
    if (180 == offsetSummer && 180 == offsetWinter) return "Africa/Nairobi";
    if (240 == offsetSummer && 180 == offsetWinter) return "Europe/Moscow";
    if (240 == offsetSummer && 240 == offsetWinter) return "Asia/Dubai";
    if (270 == offsetSummer && 210 == offsetWinter) return "Asia/Tehran";
    if (270 == offsetSummer && 270 == offsetWinter) return "Asia/Kabul";
    if (300 == offsetSummer && 240 == offsetWinter) return "Asia/Baku";
    if (300 == offsetSummer && 300 == offsetWinter) return "Asia/Karachi";
    if (330 == offsetSummer && 330 == offsetWinter) return "Asia/Calcutta";
    if (345 == offsetSummer && 345 == offsetWinter) return "Asia/Katmandu";
    if (360 == offsetSummer && 300 == offsetWinter) return "Asia/Yekaterinburg";
    if (360 == offsetSummer && 360 == offsetWinter) return "Asia/Colombo";
    if (390 == offsetSummer && 390 == offsetWinter) return "Asia/Rangoon";
    if (420 == offsetSummer && 360 == offsetWinter) return "Asia/Almaty";
    if (420 == offsetSummer && 420 == offsetWinter) return "Asia/Bangkok";
    if (480 == offsetSummer && 420 == offsetWinter) return "Asia/Krasnoyarsk";
    if (480 == offsetSummer && 480 == offsetWinter) return "Australia/Perth";
    if (540 == offsetSummer && 480 == offsetWinter) return "Asia/Irkutsk";
    if (540 == offsetSummer && 540 == offsetWinter) return "Asia/Tokyo";
    if (570 == offsetSummer && 570 == offsetWinter) return "Australia/Darwin";
    if (570 == offsetSummer && 630 == offsetWinter) return "Australia/Adelaide";
    if (600 == offsetSummer && 540 == offsetWinter) return "Asia/Yakutsk";
    if (600 == offsetSummer && 600 == offsetWinter) return "Australia/Brisbane";
    if (600 == offsetSummer && 660 == offsetWinter) return "Australia/Sydney";
    if (630 == offsetSummer && 660 == offsetWinter) return "Australia/Lord_Howe";
    if (660 == offsetSummer && 600 == offsetWinter) return "Asia/Vladivostok";
    if (660 == offsetSummer && 660 == offsetWinter) return "Pacific/Guadalcanal";
    if (690 == offsetSummer && 690 == offsetWinter) return "Pacific/Norfolk";
    if (720 == offsetSummer && 660 == offsetWinter) return "Asia/Magadan";
    if (720 == offsetSummer && 720 == offsetWinter) return "Pacific/Fiji";
    if (720 == offsetSummer && 780 == offsetWinter) return "Pacific/Auckland";
    if (765 == offsetSummer && 825 == offsetWinter) return "Pacific/Chatham";
    if (780 == offsetSummer && 780 == offsetWinter) return "Pacific/Enderbury";
    if (840 == offsetSummer && 840 == offsetWinter) return "Pacific/Kiritimati";
    return null;
  }
  
  public static <T> T[] concat(T[] first, T[] second) {
    final T[] result = Arrays.copyOf(first, first.length + second.length);
    System.arraycopy(second, 0, result, first.length, second.length);
    return result;
  }  
  
  public static byte[] concat(byte[] first, byte[] second) {
    final byte[] result = Arrays.copyOf(first, first.length + second.length);
    System.arraycopy(second, 0, result, first.length, second.length);
    return result;
  }  
  
  public static byte[] intToByteArray(int value) {
    return new byte[] {
      (byte) (value >>> 24),
      (byte) (value >>> 16),
      (byte) (value >>> 8),
      (byte) value
    };
  }
  
  public static String replicate(final String str, final int count) {
    final StringBuilder sb = new StringBuilder(count * str.length());
    for (int i = 1; i <= count; i++)
      sb.append(str);
    return sb.toString();
  } 
  
  public static Boolean fileAccessible(final String fileName) {
    return fileAccessible(new File(fileName));
  }
  
  public static Boolean fileAccessible(final File file) {
    try {
      return file.exists();
    } catch(SecurityException e) {
      return false;
    }
  }
  
  /*public static String convertStreamToString(final InputStream inputStream)
    throws IOException 
  {
    if (inputStream == null)
      return null;
    
    final Writer writer = new StringWriter();
    try {
      final Reader reader = new BufferedReader(
        new InputStreamReader(inputStream, UTF_8.name()));
      
      final char[] buffer = new char[1024];
      int n;
      while ((n = reader.read(buffer)) != -1)
        writer.write(buffer, 0, n);
    } finally {
      inputStream.close();
    }
    
    return writer.toString();
  }*/
  
}
